//
//  MianTypeListNextPageViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/11.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MianTypeListNextPageViewModel.h"
#import "MainTypesListTableViewCell.h"
#import "InputCommentViewModel.h"
#import "MainTypesListHeaderView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "MainTypesListTopTableViewCell.h"
#import "MainTypesListNextSteelMillsModel.h"

@interface MianTypeListNextPageViewModel ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    NSMutableArray *rightSelecShowArr;   // 选中的cell显示选中:图片
    InputCommentViewModel *boViewModel;
    GLGWButton *bottomBtn;
}
@property (nonatomic, strong) NSMutableArray *selectDataArr;                       // 选中的cell在顶部显示数据
@property (nonatomic, strong) NSMutableArray *selectDataModelArr;                  // 选中的cell在顶部显示数据model:indexPath
@property (nonatomic, strong) MainTypesListNextModel *model;                       // 品类：品类中的model
@property (nonatomic, strong) MainTypesListNextSteelMillsModel *stleelMillsModel;  // 钢厂：钢铁类的model

@end

@implementation MianTypeListNextPageViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        selfTableView = tableView;
        selfTableView.delegate = self;
        selfTableView.dataSource = self;
        selfTableView.backgroundColor = [UIColor whiteColor];
        selfTableView.mj_footer.hidden = YES;
        selfTableView.mj_header.hidden = YES;
        selfTableView.estimatedRowHeight = 40;
        [selfTableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [selfTableView registerClass:[MainTypesListHeaderView class] forHeaderFooterViewReuseIdentifier:@"MainTypesListHeaderView"];
        [superView addSubview:selfTableView];
        
        bottomBtn = [[GLGWButton alloc] initWithTitleLableFontType:SYSTEMFONT(16) titleColor:[UIColor colorWithHexString:@"#FFFFFF"] inSuperView:superView];
        [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(superView);
            make.bottom.equalTo(self->selfTableView.mas_bottom);
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(44)));
        }];
        [bottomBtn setBackgroundImage:[UIImage imageWithStartColor:[UIColor colorWithHexString:@"#A89A60"] endColor:[UIColor colorWithHexString:@"#A89A60" alpha:0.8]] forState:UIControlStateNormal];
        [bottomBtn setTitle:@"下一步选择主营钢厂" forState:UIControlStateNormal];
        [bottomBtn addTarget:self action:@selector(bottomAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)setType:(ChooseType)type
{
    _type = type;
    if (type == ChooseType_SteelMills) {
        [bottomBtn setTitle:@"完成" forState:UIControlStateNormal];
    }
}
// 下一步
- (void)bottomAction:(GLGWButton *)sender
{
    NSMutableArray *arr = [NSMutableArray array];
    if (self.type == ChooseType_Main) {
        for (int i = 0; i < self.selectDataModelArr.count; i ++) {
            int cout = [self.selectDataModelArr[i] intValue];
            MainTypesListNextArrModel * nextArrModel = self.model.data[cout];
            [arr addObject:nextArrModel];
        }
        if (arr.count <= 0) {
            DLog(@"没有选择钢厂，请选择一个");
            return;
        }
        if (self.BlockSelectRow) { // 点击下一步去钢厂
            NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
            muDic[@"isSelect"] = self.selectDataModelArr;
            muDic[@"arr"] = arr;
            self.dic[@"two"] = muDic;
            self.BlockSelectRow(self.dic);
        }
    } else {
        for (int i = 0; i < self.selectDataModelArr.count; i ++) {
            int cout = [self.selectDataModelArr[i] intValue];
            MainTypesListNextSteelMillsArrModel * nextArrModel = self.stleelMillsModel.data[cout];
            [arr addObject:nextArrModel];
        }
        if (arr.count <= 0) {
            DLog(@"没有选择钢厂，请选择一个");
            return;
        }
        if (self.BlockSelectRow) {  // 点击完成
            self.dic[@"three"] = arr;
            self.BlockSelectRow(self.dic);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(10);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section != 0) {
        return UITableViewAutomaticDimension;
//    }else{
        
//        return
//    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MainTypesListHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MainTypesListHeaderView"];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footer;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (self.type == ChooseType_Main) {
        if (_model) {
            return _model.data.count;
        }
    }
    if (_stleelMillsModel) {
        return _stleelMillsModel.data.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MainTypesListTopTableViewCell *cell = [MainTypesListTopTableViewCell cellWithTableView:tableView];
        cell.arr = self.selectDataArr;
        return cell;
    }
    MainTypesListTableViewCell *cell = [MainTypesListTableViewCell cellWithTableView:tableView];
    cell.showPush = NO;
    cell.hidenRighImgV = [rightSelecShowArr[indexPath.row] boolValue];
    if (self.type == ChooseType_Main) {
        if (_model) {
            MainTypesListNextArrModel *arrModel = _model.data[indexPath.row];
            cell.leftStr = arrModel.productName;
        }
    } else {
        if (self.stleelMillsModel) {
            MainTypesListNextSteelMillsArrModel *arrModel = _stleelMillsModel.data[indexPath.row];
            cell.leftStr = arrModel.factoryName;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!(indexPath.section == 1)) return;
    NSString *str = rightSelecShowArr[indexPath.row];
    if ([str isEqualToString:@"0"]) { // 显示选中
        rightSelecShowArr[indexPath.row] = @"1";
        if (self.type == ChooseType_Main) {
            MainTypesListNextArrModel *arrModel = _model.data[indexPath.row];
            [self.selectDataArr removeObject:arrModel.productName];
        } else {
            MainTypesListNextSteelMillsArrModel *arrModel = _stleelMillsModel.data[indexPath.row];
            [self.selectDataArr removeObject:arrModel.factoryName];
        }
        [self.selectDataModelArr removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    } else {
        rightSelecShowArr[indexPath.row] = @"0";
        if (self.type == ChooseType_Main) {
            MainTypesListNextArrModel *arrModel = _model.data[indexPath.row];
            [self.selectDataArr addObject:arrModel.productName];
            
        } else {
            MainTypesListNextSteelMillsArrModel *arrModel = _stleelMillsModel.data[indexPath.row];
            [self.selectDataArr addObject:arrModel.factoryName];
        }
        [self.selectDataModelArr addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    }
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
//    MainTypesListTopTableViewCell *cell = [tableView cellForRowAtIndexPath:index];
//    cell.arr = self.selectDataArr;
    [selfTableView reloadRowsAtIndexPaths:@[index,indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSMutableArray *)selectDataArr
{
    if (!_selectDataArr) {
        _selectDataArr = [NSMutableArray array];
    }
    return _selectDataArr;
}

- (NSMutableArray *)selectDataModelArr
{
    if (!_selectDataModelArr) {
        _selectDataModelArr = [NSMutableArray array];
    }
    return _selectDataModelArr;
}

- (void)setDic:(NSMutableDictionary *)dic
{
    _dic = dic;
    if (self.type == ChooseType_Main) {
        [self requestMainTypeNextDetailList];
    } else {
        [self requestMainTypeSteelMillslList];
    }
}


#pragma mark - 请求接口
- (void)requestMainTypeNextDetailList
{
    MainTypesListArrModel *model = self.dic[@"one"];
    NSDictionary *params = @{@"categoryCode" : koString(model.categoryCode)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kCategoryNextList] lodingView:nil lodingTextString:@"" isLoding:YES parameters:params success:^(id  _Nullable response) {
        NSDictionary * dic = response;
        [self makeData:dic];
    } failure:^(NSError * _Nullable error) {
        DLog(@"分类=%@",error);
    }];
}

- (void)makeData:(NSDictionary *)dic
{
    self.model = [MainTypesListNextModel yy_modelWithJSON:dic];
    rightSelecShowArr = [NSMutableArray array];
    for (int i = 0; i < _model.data.count; i ++) {
        [rightSelecShowArr addObject:@"1"];
    }
    if (self.dic[@"two"]) {
        NSDictionary *liDic = self.dic[@"two"];
        if (liDic[@"isSelect"]) {
            NSArray *arr = liDic[@"isSelect"];
            [self refreshSelectData:arr];
        }
    }
    [selfTableView reloadData];
}

- (void)refreshSelectData:(NSArray *)arr
{
    for (int i = 0; i < arr.count; i ++) {
        NSUInteger cout = [arr[i] integerValue];
        [rightSelecShowArr replaceObjectAtIndex:cout withObject:@"0"];
        MainTypesListNextArrModel *arrModel = _model.data[cout];
        [self.selectDataArr addObject:arrModel.productName];
    }
    self.selectDataModelArr = [arr mutableCopy];
}


- (void)requestMainTypeSteelMillslList
{
    NSMutableDictionary *dic = self.dic[@"two"];
    NSArray *arr = [NSArray arrayWithArray:dic[@"arr"]];
    NSMutableString *muStr;
    for (int i = 0; i < arr.count; i ++) {
        MainTypesListNextArrModel *model = arr[i];
        if (i == arr.count - 1) {
            [muStr appendFormat:@"%@",model.productId];
        } else {
            [muStr appendFormat:@"%@,",model.productId];
        }
    }
    NSDictionary *params = @{@"productId" : koString(muStr)};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kCategorySteelMills] lodingView:nil lodingTextString:@"" isLoding:YES parameters:params success:^(id  _Nullable response) {
        NSDictionary * dic = response;
        [self makeSteelMillsData:dic];
    } failure:^(NSError * _Nullable error) {
        DLog(@"分类=%@",error);
    }];
}

- (void)makeSteelMillsData:(NSDictionary *)dic
{
    self.stleelMillsModel = [MainTypesListNextSteelMillsModel yy_modelWithJSON:dic];
    rightSelecShowArr = [NSMutableArray array];
    for (int i = 0; i < _stleelMillsModel.data.count; i ++) {
        [rightSelecShowArr addObject:@"1"];
    }
    [selfTableView reloadData];
}

@end
